package com.atguigu.gmall.list.comtroller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.list.service.ListService;
import com.atguigu.gmall.model.list.Goods;
import com.atguigu.gmall.model.list.SearchParam;
import com.atguigu.gmall.model.list.SearchResponseVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.web.bind.annotation.*;

/**
 * @author Administrator
 * @create 2020-05-19 19:40
 */
@RestController
@RequestMapping("/api/list")
public class ESMappingController {

    @Autowired
    private ListService listService;

    @Autowired
    private ElasticsearchRestTemplate elasticsearchRestTemplate;
    //ElasticsearchRestTemplate：在搜索方面性能特别好，在添加、删除、更新方面不行

    @GetMapping("inner/createIndex")
    public Result createIndex() {
        elasticsearchRestTemplate.createIndex(Goods.class);//创建索引库 根据Goods
        elasticsearchRestTemplate.putMapping(Goods.class);// 映射mapping
        return Result.ok();
    }

    //商品上架添加全文搜索
    //上架商品 保存索引库
    @GetMapping("/inner/upperGoods/{skuId}")
    public Result upperGoods(@PathVariable(name = "skuId") Long skuId){
       listService.upperGoods(skuId);
        return Result.ok("上架成功");
    }
    //下架商品时删除索引库存
    @GetMapping("/inner/lowerGoods/{skuId}")
    public Result lowerGoods(@PathVariable(name = "skuId") Long skuId){
        listService.lowerGoods(skuId);
        return Result.ok("下架成功");
    }
    //更新索引的热度
    @GetMapping("inner/incrHotScore/{skuId}")
    public Result incrHotScore(@PathVariable(name = "skuId") Long skuId){
        listService.incrHotScore(skuId);
        return Result.ok();
    }

    //开始执行搜索
    //返回值    SearchResponseVo  4部分数据
    //   SearchParam 入参  6部分数据
    @PostMapping
    public SearchResponseVo list(@RequestBody SearchParam searchParam){

        return listService.list(searchParam);
    }

}
